//********************************************************************************************************************//
//ORGANIZATION:
//AUTHOR: bpeng(bogers.peng@gmail.com)
//SUMMARY:
//********************************************************************************************************************//
#include "../Header.h"
#include "../Variable.h"
#include "System/Base/Exceptions.h"

using namespace Native::Compiling;
using namespace Native::Compiling::Sql;

CHeader::CHeader()
{
}

CHeader::CHeader(C_HEADER& that) :
    CField(that),
    _name(that._name),
    _return_type(that._return_type),
    _variables(that._variables)
{
}

CHeader::CHeader(C_HEADER&& that) :
    CField(that),
    _name(that._name),
    _return_type(that._return_type),
    _variables(that._variables)
{
}

CHeader::~CHeader()
{
}

wstring CHeader::ToString() const
{
    return EMPTY_STRING;
}

bool CHeader::operator==(C_HEADER& rvalue) const
{
    return false;
}

bool CHeader::operator!=(C_HEADER& rvalue) const
{
    return !operator==(rvalue);
}

void CHeader::Clear()
{
    CField::Clear();

    _name = EMPTY_STRING;
    _return_type = EMPTY_STRING;
    _variables.Clear();
}

CString CHeader::ToCode() const
{
    return EMPTY_STRING;
}

void CHeader::Append(C_VARIABLE& variable) throw()
{
    try {
        _variables.Add(variable.Name(), variable);
    } catch(CException& e) {
        NATIVE_THROW_CAUGHT(L"Append", e);
    }
};

CArray<CVariable> CHeader::Query(C_STRING& name) const throw()
{
    CArray<CVariable> queried;
    auto cursor = _variables.Find(name);

    try {
        if (cursor.Exist()) {
            queried.Append(_variables.Value(cursor.Key()));
        }
        return move(queried);
    } catch(CException& e) {
        NATIVE_THROW_CAUGHT(L"Query", e);
    }
};

C_HEADER& CHeader::operator=(C_HEADER& rvalue)
{
    CField::operator=(rvalue);

    _name = rvalue._name;
    _return_type = rvalue._return_type;
    _variables = rvalue._variables;

    return *this;
}